package com.yc.framework.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Value("${file.upload.path}")
    private String path;
    @Value("${file.upload.relativePath}")
    private String relativePath;

    //WebMvcConfigurerAdapter
    @Bean
    public WebMvcConfigurer webMvcConfigurer () {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
//                System.out.println("资源映射:"+relativePath + "/** -> file:"+ path);
                // 以“/files/**”访问资源的请求，转到“file:d:\\upload\\”的真实地址
                //静态资源的访问配置
                registry.addResourceHandler(relativePath + "/**")
                        .addResourceLocations("file:" + path);
//                        /home/javafiles
//                    .addResourceLocations("file:" + File.separator + "home" + File.separator + "javafiles" + File.separator );
                WebMvcConfigurer.super.addResourceHandlers(registry);
            }
        };
    }
}
